<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport"
    content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars3d.cn " />
  <meta name="apple-touch-fullscreen" content="yes" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="x5-fullscreen" content="true" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
    content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="/img/favicon/favicon.ico" />
  <title>动画脚本演示 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

  <!--第三方lib-->
  <script type="text/javascript" src="/lib/include-lib.js" libpath="/lib/"
    include="jquery,layer,toastr,bootstrap,bootstrap-checkbox,font-awesome,web-icons,jstree,layer,haoutil,nprogress,toastr,admui,mars3d,mars3d-space"></script>

  <link href="/css/style.css" rel="stylesheet" />
  <link href="./map.css" rel="stylesheet" />
  <style>
    .viewResult {
      top: 10px;
      left: 10px;
      bottom: auto;
      right: auto;
      max-width: 420px;
      overflow: auto;
      font-size: 28px;
    }

    .contentUl {
      padding: 0;
      text-align: left;
      font-size: 20px;
      margin-left: 10px;
    }

    .btn i {
      margin-right: 5px;
    }

    .btn-right5 {
      margin-right: 5px;
    }
  </style>
</head>

<body class="dark">
  <div id="mars3dContainer" class="mars3d-container"></div>

  <div id="viewReset" class="infoview" style="overflow: auto; top: 10px">
    <div class="btn-group">
      <button id="btn_start" type="button" class="btn btn-primary"><i class="fa fa-play-circle-o"></i>开始</button>
      <button id="btn_pause" type="button" class="btn btn-primary btn-right5" style="display: none"><i
          class="fa fa-pause-circle-o"></i>暂停</button>
      <button id="btn_proceed" type="button" class="btn btn-primary btn-right5" style="display: none"><i
          class="fa fa-play-circle-o"></i>继续</button>
      <button id="btn_stop" type="button" class="btn btn-primary" style="display: none"><i
          class="fa fa-stopFun-circle-o"></i>停止</button>
    </div>

    <ul id="treeOverlays" style="padding: 0"></ul>
    <br />
    总时长: <span id="alltimes">0s</span> &nbsp;&nbsp;
    <br />
    当前:&nbsp;&nbsp;<span id="thisStep">无</span> &nbsp;&nbsp;
    <span id="thistimes"></span>
  </div>

  <script src="/js/common.js"></script>

  <script src="./map.js"></script>
  <script src="./m35_dhjb.js"></script>

  <script type="text/javascript">
    "use script" //开发环境建议开启严格模式

    let isPlay = false
    let isPause = false
    let totalTimes = ""
    let currentWork = ""
    let counter = 0
    let selectedKeys
    let animations = []
    let currentIndex
    let timer
    let interval

    //UI界面相关
    function initUI() {
      initEditorJS(map)

      $("#btn_start").click(function () {
        dataWork.start()
        // play()
      })

      $("#btn_pause").click(function () {
        dataWork.pause()
        pause()
      })

      $("#btn_proceed").click(function () {
        dataWork.proceed()

        $("#btn_start").hide()
        $("#btn_pause").show()
        $("#btn_proceed").hide()
        $("#btn_stop").show()
      })

      $("#btn_stop").click(function () {
        stopFun()
      })

      let i = 0
      let time = 0
      treeData.forEach((item) => {
        animations = animations.concat(
          item.children.map((it) => {
            time += it.times
            it.index = i
            i++

            return it
          })
        )
      })
      console.log(animations)
      totalTimes = `${Math.floor(time / 60)}分${time % 60}秒`

      map.on(mars3d.EventType.keydown, (event) => {
        switch (event.keyCode) {
          default:
            break
          case "1".charCodeAt(0): //播放、暂停切换
            if (dataWork._isStart) {
              $("#btn_pause").click()
            } else {
              $("#btn_proceed").click()
            }
            break
          case "2".charCodeAt(0): //停止
            $("#btn_stop").click()
            break
          case "3".charCodeAt(0): //绑定和解绑键盘(控制器)控制
            map.keyboardRoam.enabled = !map.keyboardRoam.enabled
            if (map.keyboardRoam.enabled) {
              haoutil.msg("已开启键盘控制")
            } else {
              haoutil.msg("已关闭键盘控制，您可以鼠标进行操作了！")
            }
            break
        }
      })
    }

    const play = () => {
      this.isPlay = true
      this.isPause = false
      start()

      $("#btn_start").hide()
      $("#btn_pause").show()
      $("#btn_proceed").hide()
      $("#btn_stop").show()
    }

    const pause = () => {
      clearTimeout(timer)
      this.currentIndex--
      this.isPause = true

      $("#btn_start").hide()
      $("#btn_pause").hide()
      $("#btn_proceed").show()
      $("#btn_stop").show()
    }

    const stopFun = () => {
      this.isPlay = false
      this.isPause = false
      this.currentIndex = 0
      clearTimeout(timer)
      clearInterval(interval)
      dataWork.stopFun()

      $("#btn_start").show()
      $("#btn_pause").hide()
      $("#btn_stop").hide()
      $("#btn_proceed").hide()

      $("#thisStep").html("无")
      $("#thistimes").html("")
      $("#treeOverlays").jstree("deselect_all", true)
    }

    const start = () => {
      if (timer) {
        clearTimeout(timer)
      }
      if (interval) {
        clearInterval(interval)
      }
      if (currentIndex < animations.length) {
        const animate = animations[currentIndex]
        selectedKeys = [animate.key]
        currentWork = `${animate.title}(${animate.times}秒)`
        counter = animate.times
        countOn()
        animate.widget()
        currentIndex++
        timer = setTimeout(() => {
          start()
        }, animate.times * 1000)
      } else {
        stopFun()
      }
    }

    function countOn() {
      interval = setInterval(() => {
        counter--
        if (counter <= 0) {
          clearInterval(interval)
        }
      }, 1000)
    }

    const treeData = [
      {
        title: "卫星推演",
        key: "01",
        children: [
          {
            text: "初始化场景",
            times: 2
          },
          {
            text: "需求受理",
            times: 8
          },
          {
            text: "任务编排",
            times: 7
          },
          {
            text: "任务上注",
            times: 10
          },
          {
            text: "卫星观测",
            times: 15
          },
          {
            text: "数据接收",
            times: 10
          },
          {
            text: "数据传输",
            times: 10
          },
          {
            text: "产品生产",
            times: 5
          },
          {
            text: "产品分发",
            times: 5
          }
        ]
      }
    ]

    // //数据处理，及自动播放
    let dataWork = {
      alltimes: 0,
      arrNode: [],
      analysisData: function (arr) {
        for (let i = 0; i < arr.length; i++) {
          let item = arr[i]
          // item.state = { disabled: true }; //前端播放时禁用单击

          if (item.widget) {
            item.index = this.arrNode.length //this.getNextId();
            item.id = item.index
            item.times = item.times || 60
            item.text = item.text + "(" + item.times + "秒)"
            item.icon = "fa fa-tag"
            this.arrNode.push(item)
            this.alltimes += item.times
          }

          if (item.children) {
            this.analysisData(item.children)
          }
        }
      },
      steptimes: 0,
      initData: function (data) {
        this.analysisData(data)
        $("#alltimes").html(haoutil.str.formatTime(this.alltimes))

        setInterval(() => {
          if (!this._isStart) {
            return
          }
          this.steptimes++
          $("#thistimes").html(this.steptimes + "秒")
        }, 1000)

        $("#treeOverlays").jstree({
          core: {
            data: data,
            themes: {
              name: "default-dark",
              dots: true,
              icons: true
            }
          }
        })

        $("#treeOverlays").on("changed.jstree", (e, data) => {
          let node = data.node.original
          if (node && node.widget) {
            this.start(node)
          }
        })
      },
      _isStart: false,
      selectdNode: null,
      //开始
      start: function (node) {
        this.stopFun()

        $("#btn_start").hide()
        $("#btn_pause").show()
        $("#btn_proceed").hide()
        $("#btn_stop").show()

        this._isStart = true
        // map.clock.shouldAnimate = true
        dataWork.activateNode(node || this.arrNode[0])
      },
      //暂停
      pause: function () {
        this._isStart = false

        // map.clock.shouldAnimate = false

        if (this.timeIdx && this.timeIdx != -1) {
          clearTimeout(this.timeIdx)
          this.timeIdx = -1
        }
      },
      //   //继续
      proceed: function () {
        // map.clock.shouldAnimate = true

        let node = this.selectdNode
        if (node) {
          let that = this

          this.steptimes = 0
          node.widget.disable()
          node.widget.activate()

          this.timeIdx = setTimeout(function () {
            node.widget.disable()
            that.activateNextNode(node.index)
          }, node.times * 1000)
        } else {
          this.start()
        }
        this._isStart = true
      },

      //停止
      stopFun: function () {
        this.pause()

        if (this.selectdNode) {
          this.selectdNode.widget.disable()
        }
        dataWork.selectdNode = null
        this._isStart = false

        clearAll()

        JB.closePanel()
      },
      activateNode: function (node) {
        this.selectdNode = node
        $("#stopRoate").val("暂停")
        this.steptimes = 0
        $("#thisStep").html(node.text)
        $("#treeOverlays").jstree("deselect_all", true)
        $("#treeOverlays").jstree("select_node", node.id, true)
        node.widget.activate(node)

        // $("#treeOverlays").jstree().close_all(); //收起树

        let that = this
        this.timeIdx = setTimeout(function () {
          node.widget.disable()
          that.activateNextNode(node.index)
        }, node.times * 1000)
      },
      activateNextNode: function (index) {
        index++
        if (index < 0 || index >= this.arrNode.length) {
          this.stopFun()
          this.selectdNode = null
          return
        }
        let node = this.arrNode[index]
        this.activateNode(node)
      }
    }

    //通用方法 脚本列表
    let JB = {
      showPanel(html) {
        $("#viewResult").remove()
        let innerHTML = `<div id="viewResult" class="infoview viewResult" >
            ${html}
        </div>`
        $("body").append(innerHTML)
      },
      closePanel() {
        $("#viewResult").remove()
      },
      playMP3(src) {
        let mp3 = new Audio(src)
        mp3.play() //播放 mp3这个音频对象
        return mp3
      }
    }
  </script>
</body>

</html>
